@page "/chat"
@using Volo.Abp.MultiTenancy
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication 
@using Microsoft.AspNetCore.SignalR.Client
@using Microsoft.Extensions.Configuration

@inherits SignalRDemoBlazorUiComponentBase

@attribute [Authorize]

<div class="container">
    <h1>Chat</h1>
    <div>
        <div class="row">
            <div class="col-md-6">
                <div>All Messages:</div>
                <ul id="MessageList" style="">
                    @foreach(var message in Messages)
                    {
                        if(message.user == CurrentUser.UserName)
                        {
                            <li><i class="fas fa-long-arrow-alt-left"></i> @message.user: @message.message</li>
                        }
                        else
                        {
                            <li><strong><i class="fas fa-long-arrow-alt-right"></i> @message.user: @message.message</strong></li>
                        }
                    }
                </ul>
            </div>
            <div class="col-md-6">
                <form>
                    <div class="row">
                        <div class="col">
                            <label for="TargetUser">Target user:</label>
                            <InputText id="TargetUser" @bind-Value="TargetUser"/>
                        </div>
                    </div>
                    <div class="mt-2 row">
                        <div class="col">
                            <label for="Message">Message:</label>
                            <InputTextArea class="ms-2" id="Message" @bind-Value="UserMessage" rows="4"/>
                        </div>
                    </div>
                    <div class="mt-3 row">
                        <div class="col">
                            <Button @onclick="() => SendMessageAsync()" class="btn btn-primary" data-busy-text="Processing..."><span>SEND!</span></Button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

@code{

    [Inject]
    public IAccessTokenProvider AccessTokenProvider { get; set; } = default!;

    [Inject]
    public IConfiguration Configuration { get; set; } = default!;

    public HubConnection HubConnection { get; set; } = default!;

    public string UserMessage { get; set; } = "";

    public string TargetUser {get; set;} = "";

    public List<(string user, string message)> Messages { get; set; } = new List<(string user, string message)>();

    protected async override Task OnInitializedAsync()
    {
        (await AccessTokenProvider.RequestAccessToken()).TryGetToken(out var accessToken);
        var signalRUrl = Configuration["ChatUrl"]!;

        HubConnection = new HubConnectionBuilder()
            .WithUrl(signalRUrl.EnsureEndsWith('/') + "signalr-hubs/chat", options =>
            {
                options.AccessTokenProvider = () => Task.FromResult((string?)accessToken!.Value);
            })
            .Build();

        HubConnection.On<string, string>("ReceiveMessage", async (user, message) =>
        {
            Messages.Add((user, message));
            await InvokeAsync(StateHasChanged);
        });

        await HubConnection.StartAsync();
    }

    public async Task SendMessageAsync()
    {
        if(string.IsNullOrEmpty(UserMessage))
        {
            await Notify.Warn("Please enter a message!");
            return;
        }

        if(string.IsNullOrEmpty(TargetUser))
        {
            await Notify.Warn("Please enter a target user!");
            return;
        }

        await HubConnection.InvokeAsync("SendMessage", TargetUser, UserMessage);
        Messages.Add((CurrentUser.UserName!, UserMessage));
        UserMessage = "";
    }
}